#include "formatter.hpp"
#include"sink.hpp"
#include<string>
#include"logger.hpp"
#include"looper.hpp"
#include"log.h"


void test_formatter()
{
	std::string name="root";
	Log::LogMsg msg(name,"test.cpp",38,"测试",Log::LogLevel::value::WARN);
	
	Log::Formatter formatter("%d{%H:%M:%S}[%p][%c][%f:%l]%T%m%n");
	std::cout << formatter.format(msg) << std::endl;
	
}

void test_sink()
{
	using namespace Log;
	std::string name="root";
	Log::LogMsg msg(name,"test.cpp",38,"测试",Log::LogLevel::value::WARN);
	Log::Formatter formatter("%d{%H:%M:%S}[%p][%c][%f:%l]%T%m%n");

	
	LogSink::ptr logSink = SinkFactory::create<FileSink>("./logfile/test.txt");

	// 生成日志
	std::string s = formatter.format(msg);
	LogSink::ptr log_out = SinkFactory::create<StdoutSink>();
	LogSink::ptr log_file = SinkFactory::create<FileSink>("./logfile/test.txt");

	// 使用落地方向进行输出
	log_out->log(s.c_str(), s.size());
	log_file->log(s.c_str(), s.size());
	
}

void test_logger()
{
	
	using namespace Log;
	// 创建格式化器
	Log::Formatter::ptr formatter = std::make_shared<Formatter>();
	// 创建落地方向对象
	LogSink::ptr log_out = SinkFactory::create<StdoutSink>();
	LogSink::ptr log_file = SinkFactory::create<FileSink>("./logfile/test");
	// 创建落地方向数组
	std::vector<LogSink::ptr> sinks = { log_out, log_file };
	// 创建日志器
	std::string logger_name = "slogger";
	SyncLogger slogger(logger_name, formatter, sinks, LogLevel::value::DEBUG);

	
	// 输出日志
	slogger.error(__FILE__, __LINE__, "测试日志");
	
	
}


void test_builder()
{
	using namespace Log;
	LocalLoggerBuilder local_builder;
	// 切记日志器不能没有名字！
	local_builder.buildLoggerName("local_logger");
	// 设置日志器类型
	local_builder.buildLoggerType(Logger::Type::LOGGER_SYNC);
	// 设置落地方向
	local_builder.buildSink<FileSink>("./test.log");
	// 设置日志输出格式
	local_builder.buildFormatter("[%d{%H:%M:%S}][%p][%f:%l]%T%m%n");
	// 构造日志器
	Logger::ptr default_logger = local_builder.build();
	// 进行日志输出
	default_logger->error(__FILE__, __LINE__, "测试日志器建造者类");

}




using namespace Log;



void log_test()
{
	
	Logger::ptr logger =loggerManager::getInstance().getLogger("async_logger");
	logger->debug(__FILE__, __LINE__, "%s", "测试日志");
	logger->info(__FILE__, __LINE__, "%s", "测试日志");
	logger->warn(__FILE__, __LINE__, "%s", "测试日志");
	logger->error(__FILE__, __LINE__, "%s", "测试日志");
	logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

	size_t count = 0;
	while (count < 300)
	{
		logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
	}
}

void test_async()
{
	
	// 创建全局日志器建造者
	std::unique_ptr<Log::Logger::Builder> builder(new Log::GlobalLoggerBuilder());
	// 构造内部组件
	builder->buildLoggerName("async_logger");
	builder->buildLoggerLevel(LogLevel::value::WARN);
	builder->buildFormatter("[%c]%m%n");
	builder->buildLoggerType(Log::Logger::Type::LOGGER_ASYNC);
	builder->buildSink<FileSink>("./logfile/async.log");
	builder->buildSink<StdoutSink>();
	// 构造出日志器对象并将其添加到日志器管理单例中
	builder->build();
	log_test();
}


//全局接口测试：
void test_global()
{
	LOGD("%s", "测试日志");
	LOGI("%s", "测试日志");
	LOGW("%s", "测试日志");
	LOGE("%s", "测试日志");
	LOGF("%s", "测试日志");

	size_t count = 0;
	while (count < 30)
	{
		LOGF("测试日志-%d", count++);
	}
	
}



int main()
{
	//test_formatter();
	//test_sink();
	//test_logger();
	//test_builder();
	//test_async();
    test_global();
	return 0;
    
}


